package cn.iocoder.yudao.module.polling.controller.admin.pollingproject;

import cn.hutool.core.io.IoUtil;
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import cn.iocoder.yudao.module.polling.controller.admin.pollingproject.vo.*;
import cn.iocoder.yudao.module.polling.convert.ids.OrderUtil;
import cn.iocoder.yudao.module.polling.convert.pollingproject.PollingProjectConvert;
import cn.iocoder.yudao.module.polling.dal.dataobject.pollingproject.PollingProjectDO;
import cn.iocoder.yudao.module.polling.service.pollingproject.PollingProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_IS_EMPTY;


@Api(tags = "管理后台 - 巡检项目")
@RestController
@RequestMapping("/abu/polling-project")
@Validated
public class PollingProjectController {

    @Resource
    private PollingProjectService pollingProjectService;

    @Resource
    private FileApi fileApi;


    @PostMapping("/create")
    @ApiOperation("创建巡检项目")
    @PreAuthorize("@ss.hasPermission('abu:polling-project:create')")
    public CommonResult<Integer> createPollingProject(@Valid @RequestBody PollingProjectCreateReqVO createReqVO) {
        createReqVO.setPpCode(OrderUtil.getId("PL"));
        return success(pollingProjectService.createPollingProject(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新巡检项目")
    @PreAuthorize("@ss.hasPermission('abu:polling-project:update')")
    public CommonResult<Boolean> updatePollingProject(@Valid @RequestBody PollingProjectUpdateReqVO updateReqVO) {
        pollingProjectService.updatePollingProject(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除巡检项目")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('abu:polling-project:delete')")
    public CommonResult<Boolean> deletePollingProject(@RequestParam("id") Integer id) {
        pollingProjectService.deletePollingProject(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得巡检项目")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('abu:polling-project:query')")
    public CommonResult<PollingProjectRespVO> getPollingProject(@RequestParam("id") Integer id) {
        PollingProjectDO pollingProject = pollingProjectService.getPollingProject(id);
        return success(PollingProjectConvert.INSTANCE.convert(pollingProject));
    }


    @GetMapping("/list")
    @ApiOperation("获得巡检项目列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('abu:polling-project:query')")
    public CommonResult<List<PollingProjectRespVO>> getPollingProjectList(@RequestParam("ids") Collection<Integer> ids) {
        List<PollingProjectDO> list = pollingProjectService.getPollingProjectList(ids);
        return success(PollingProjectConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得巡检项目分页")
    @PreAuthorize("@ss.hasPermission('abu:polling-project:query')")
    public CommonResult<PageResult<PollingProjectRespVO>> getPollingProjectPage(@Valid PollingProjectPageReqVO pageVO) {
        PageResult<PollingProjectDO> pageResult = pollingProjectService.getPollingProjectPage(pageVO);
        return success(PollingProjectConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/myList")
    @ApiOperation("获得巡检项目分页")
    @PreAuthorize("@ss.hasPermission('abu:polling-project:list')")
    public CommonResult<PageResult<PollingProjectIOVO>> getPollingProjectPageMy(@Valid PollingProjectIOVO pageVO) {
        List<PollingProjectIOVO> list = pollingProjectService.selectByMyList(pageVO);
        PageResult<PollingProjectIOVO> pageResult=new PageResult<>();
        pageResult.setList(list);
        pageResult.setTotal((long) list.size());
        return success(pageResult);
    }

    @GetMapping("/myListBz")
    @ApiOperation("获得巡检项目分页bx")
    @PreAuthorize("@ss.hasPermission('abu:polling-project:listbx')")
    public CommonResult<PageResult<PollingProjectIOVO>> getPollingProjectPageBz(@Valid PollingProjectIOVO pageVO) {
        List<PollingProjectIOVO> list = pollingProjectService.selectProjectByBzId(pageVO);
        PageResult<PollingProjectIOVO> pageResult=new PageResult<>();
        pageResult.setList(list);
        pageResult.setTotal((long) list.size());
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出巡检项目 Excel")
    @PreAuthorize("@ss.hasPermission('abu:polling-project:export')")
    @OperateLog(type = EXPORT)
    public void exportPollingProjectExcel(@Valid PollingProjectExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PollingProjectDO> list = pollingProjectService.getPollingProjectList(exportReqVO);
        // 导出 Excel
        List<PollingProjectExcelVO> datas = PollingProjectConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "巡检项目.xls", "数据", PollingProjectExcelVO.class, datas);
    }

    @PostMapping(value = "/img")
    @ApiOperation("上传图片")
    public CommonResult<String> updateUserAvatar(@RequestParam("avatarFile") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw ServiceExceptionUtil.exception(FILE_IS_EMPTY);
        }
        String avatar = fileApi.createFile(IoUtil.readBytes(file.getInputStream()));
        return success(avatar);
    }

}
